let fetch = require("../../utils/fetch");
let app = getApp();
Page({
    data: {
        coupons: [],
        voucherMemberId: '',
        imageUrl: app.globalData.imageUrl,

    },
    onLoad: function(options) {
        // 生命周期函数--监听页面加载
        const voucherMemberId = options.voucherMemberId || "";
        this.setData({
            voucherMemberId
        })
        console.log(voucherMemberId)
        fetch.getNewReserveCoupons().then(response => {
            const res = response.data;
            if (res.obj.length > 0) {


                this.setData({
                    coupons: this.fitArr(res.obj)
                })
                this.autoMapping(voucherMemberId)
                app.globalData.common.reserveVid = voucherMemberId;
            }
        }).catch(error => {
            fetch.showMyToast(this, error.data.des)
        })

    },
    fitArr: function(arr) {
        console.log("***")
        console.log(arr)
        for (var i = 0; i < arr.length; i++) {

            if (!arr[i].shopNames && !arr[i].goodNames) {
                //arr[i].shopRouls = "此优惠券为通用券";
                arr[i].goodsRouls = "此优惠券为通用券；";

            } else {
                var str1 = "";
                if (arr[i].shopNames) {
                    str1 += "此优惠券只适用于指定门店："
                    for (var j = 0; j < arr[i].shopNames.length; j++) {
                        str1 += arr[i].shopNames[j];
                        str1 += "、";

                    }

                    str1 = str1.length > 0 ? str1.substring(0, str1.length - 1) + "" : "";

                } else {
                    str1 += "此优惠券适用于所有门店；"
                }

                arr[i].shopRouls = str1;
                var str2 = '';
                if (arr[i].goodNames) {
                    str2 += "此优惠券只适用于指定商品："
                    for (var j = 0; j < arr[i].goodNames.length; j++) {
                        str2 += arr[i].goodNames[j];
                        str2 += "、";
                    }
                    str2 = str2.length > 0 ? str2.substring(0, str2.length - 1) + "" : "";
                    str2 += '；'
                } else {
                    str2 += "此优惠券适用于所有商品；"
                }
                console.log("%%%%%")
                console.log(str1)
                arr[i].goodsRouls = str1 + str2;
                if (arr[i].goodsRouls.length < 30) {
                    arr[i].rouleStyle = 'nomalRoule'
                } else {
                    arr[i].rouleStyle = 'shortRoule';
                    arr[i].transform_style = 'up_style'
                }

            }

        }
        return arr;

    },
    onReady: function() {
        // 生命周期函数--监听页面初次渲染完成

    },
    onShow: function() {
        // 生命周期函数--监听页面显示

    },
    radioChange: function(e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value)
        this.setData({
            voucherMemberId: e.detail.value
        })
        app.globalData.common.reserveVid = e.detail.value;
        app.globalData.common.isAuto = '';
        wx.navigateBack({
            delta: 1
        })
    },
    cancelCoupon: function(e) {
        console.log('取消选择优惠券：', e.detail.value)
        this.setData({
            voucherMemberId: ""
        })
        app.globalData.common.reserveVid = '';
        app.globalData.common.isAuto = '1';
        this.autoMapping();
        wx.navigateBack({
            delta: 1
        })
    },
    autoMapping: function(voucherMemberId = '') {
        const coupons = this.data.coupons;
        coupons.map(c => {
            c.checked = false;
            if (!voucherMemberId) {
                return
            }
            if (voucherMemberId == c.voucherMemberId) {
                c.checked = true;
                return
            }
        })
        console.log(coupons)
        this.setData({
            coupons
        })
    },
    showDetail: function(e) {
        var id = e.target.dataset.code;
        console.log(this.data.coupons)
        for (var i = 0; i < this.data.coupons.length; i++) {
            if (id == this.data.coupons[i].voucherId) {
                if (this.data.coupons[i].rouleStyle == 'shortRoule') {
                    this.data.coupons[i].rouleStyle = 'longRoule'
                    this.data.coupons[i].transform_style = 'down_style'
                } else if (this.data.coupons[i].rouleStyle == 'longRoule') {
                    this.data.coupons[i].rouleStyle = 'shortRoule';
                    this.data.coupons[i].transform_style = 'up_style'
                }
            }
        }
        this.setData({
            coupons: this.data.coupons
        })
    }



})